const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    positiondata:{},//职位数据
    enterpriseUid:'',//企业Uid
    shengindex:0,//省索引
    shengArr:[],//省
    saveProvince:'',//上传的省
    provinceCode:'',//省编码
    shiindex:0,//市索引
    shiArr:[],//市
    saveCity:'',//上传的市
    cityCode:'',//市编码
    jyindex:0, //经验要求索引
    jingyanArr:[],//经验要求
    experience:'',///上传的经验要求
    xlindex:0, //最低学历索引
    xueliArr:[],//最低学历
    educational:'',///上传的最低学历
    xzindex:0, //薪资范围索引
    xinziArr:[],//薪资范围
    positionName:'',//职位名称
    positionDuty:'',//岗位职责
    qualifications:'',//任职资格
    zprs:'',//招聘人数
    city:'',//城市
    address:'',//详细地点
    wageRange:'',//上传的薪资范围
    hangye:'选择行业',
    workType:'',//工作类型  全职 兼职
    gswelfare:[],
    welfareval:'',//新增福利
    fuliactive:false,//福利是否选中
    fulistr:'',//
    fuliarr:[],//
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('ID',options)
    var that = this;
    var userInfo = wx.getStorageSync('userInfo')
    console.log('地址参数',userInfo)
    that.data.enterpriseUid = userInfo.enterpriseUid
    that.setData({
      id:options.id
    });
    // 省
    wx.request({
      url: url + 'area/province',
      method: "GET",
      data:{},
      success:function(res){
        console.log("省",res);
        var ret = res.data
          that.setData({
            shengArr:ret.data,//省
          })
        
      }
    })
    // 经验要求
    wx.request({
      url: url + 'dict/getWorkYearState',
      method: "GET",
      data:{},
      success:function(res){
        console.log("经验要求",res);
        var ret = res.data
          that.setData({
            jingyanArr:ret.data,//经验要求
          })
        
      }
    })
    // 最低学历
    wx.request({
      url: url + 'dict/getEducationState',
      method: "GET",
      data:{},
      success:function(res){
        console.log("最低学历",res);
        var ret = res.data
          that.setData({
            xueliArr:ret.data,//最低学历
          })
      }
    })
    // 薪资范围
    wx.request({
      url: url + 'dict/getWageRangeState',
      method: "GET",
      data:{},
      success:function(res){
        console.log("薪资范围",res);
        var ret = res.data
          that.setData({
            xinziArr:ret.data,//薪资范围
          })
      }
    })
    //职位编辑
    if(options.id > 0){
      wx.request({
        url: url + 'position/getPosition',
        method: "GET",
        data:{
          position_id:options.id,
          userUid:''
        },
        success:function(res){
          if(res.data.code == 200){
            that.data.positiondata = res.data.data;
            var newfuli = that.data.positiondata.welfareRemark.split(",")
            for(var i in newfuli){
              var newData = {
                id:i,
                name:newfuli[i],
                isSelected: true
              }
              that.data.gswelfare.push(newData);
            }
            that.setData({
              positiondata:that.data.positiondata,
              positionName:that.data.positiondata.positionName,
              address:that.data.positiondata.address,
              zprs:that.data.positiondata.zprs,
              positionDuty:that.data.positiondata.positionDuty,
              qualifications:that.data.positiondata.qualifications,
              hangye:that.data.positiondata.industry,
              wageRange:that.data.positiondata.wageRange,
              saveProvince:that.data.positiondata.saveProvince,
              saveCity:that.data.positiondata.saveCity,
              provinceCode:that.data.positiondata.provinceCode,
              cityCode:that.data.positiondata.cityCode,
              experience:that.data.positiondata.experience,
              educational:that.data.positiondata.educational,
              gswelfare:that.data.gswelfare
            })
            if(that.data.positiondata.provinceCode){
              // 市
              wx.request({
                url: url + 'area/city?province_code=' + that.data.positiondata.provinceCode.substr(0,6),
                method: "GET",
                data:{},
                success:function(res){
                  console.log("市",res);
                  var ret = res.data
                    that.setData({
                      shiArr:ret.data,//市
                    })
                }
              })
            }
            
          }
        }
      })
    }else{
      console.log('福利',userInfo.treatment)
      var newfuli = userInfo.treatment.split(",")
      for(var i in newfuli){
        var newData = {
          id:i,
          name:newfuli[i],
          isSelected: true
        }
        that.data.gswelfare.push(newData);
      }
      that.setData({
        gswelfare:that.data.gswelfare
      })      
    }
  },
  onShow(){
    var that = this
    var city = wx.getStorageSync('cityName')
    var hangye = wx.getStorageSync('hangye')
    var zhiwei = wx.getStorageSync('zhiwei')
    
    if(city){
      that.setData({city:city})
    }
    if(hangye){
      that.setData({hangye:hangye})
    }
    if(zhiwei){
      that.setData({zhiwei:zhiwei})
    }
      
  },
  // 所属行业
  hangye(e){
    var that = this
    // that.setData({hangye:e.detail.value})
    wx.navigateTo({
      url: '/HomePage/industry/industry',
    })
  },
  // 职业名称
  positionName(e){
    var that = this
    that.setData({positionName:e.detail.value})
  },
  // 岗位职责
  positionDuty(e){
    var that = this
    that.setData({positionDuty:e.detail.value})
  },
  // 任职资格
  qualifications(e){
    var that = this
    that.setData({qualifications:e.detail.value})
  },
  //招聘人数
  zprs(e){
    var that = this
    that.setData({zprs:e.detail.value})
  },
  // 详细地点
  address(e){
    var that = this
    that.setData({address:e.detail.value})
  },
  // 选择城市
  select_city(){
    wx.navigateTo({
      url: '/HomePage/selectcity/selectcity',
    })
  },
  //选择省
  shengChange: function (e){
    var that = this;
    var val = e.detail.value//这个是  index
    var code = ''//这个是  code
    var skill = that.data.shengArr
    for(let item in skill){
      if(item == val){
        that.setData({saveProvince:skill[item].name,provinceCode:skill[item].code +'|' + skill[item].name})
        code = skill[item].code
        break;
      }
    }
    console.log(val,code)
    that.setData({shengindex:val})//页面赋值 title:name
    // 市
    wx.request({
      url: url + 'area/city?province_code=' + code,
      method: "GET",
      data:{},
      success:function(res){
        console.log("市",res);
        var ret = res.data
          that.setData({
            shiArr:ret.data,//市
          })
      }
    })
  },
  //选择市
  shiChange: function (e){
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.shiArr
    for(let item in skill){
      if(item == val){
        that.setData({saveCity:skill[item].name,cityCode:skill[item].code +'|' + skill[item].name})
        // that.setData({level:skill[item].id,})
        break;
      }
    }
    console.log(val)
    that.setData({shiindex:val,})//页面赋值 title:name
  },
  //经验要求
  jingyanChange: function (e){
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.jingyanArr
    for(let item in skill){
      if(item == val){
        that.setData({experience:skill[item].subTypeName,})
        // that.setData({level:skill[item].id,})
        break;
      }
    }
    console.log(val)
    that.setData({jyindex:val,})//页面赋值 title:name
  },
  // 最低学历
  xueliChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.xueliArr
    for(let item in skill){
      if(item == val){
        that.setData({educational:skill[item].subTypeName,})
        // that.setData({level:skill[item].id,})
        break;
      }
    }
    console.log(val)
    that.setData({xlindex:val,})//页面赋值 title:name
  },
  //薪资范围
  xinziChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.xinziArr
    for(let item in skill){
      if(item == val){
        that.setData({wageRange:skill[item].subTypeName,})
        // that.setData({level:skill[item].id,})
        break;
      }
    }
    console.log(val)
    that.setData({xzindex:val,})//页面赋值 title:name
  },
  //公司福利多选
  welfareSelected: function (e) {
    let index = e.currentTarget.dataset.index;
    if(e.currentTarget.dataset.active == false){
      this.data.fuliactive = true;
    }else{
      this.data.fuliactive = false;
    }
    console.log('状态',this.data.fuliactive)
    let item = this.data.gswelfare[index];
    item.isSelected = !item.isSelected;
    this.setData({
      gswelfare: this.data.gswelfare,
    });
    var jiekou = [];
    for (var i in this.data.gswelfare){
      if(this.data.gswelfare[i].isSelected){
        jiekou.push(this.data.gswelfare[i].name);
        this.data.fuliarr = jiekou
        this.data.fulistr = jiekou.join(',')
        console.log('是否选中福利',jiekou,this.data.fuliarr,this.data.fulistr)
      } 
    }
  },
  //输入新福利
  welfarefocus(e){
    var that = this
    that.setData({welfareval:e.detail.value})
  },
  //加入备选项
  joinWelfare: function (e) {
    var that = this
    if(that.data.welfareval == ''){
      wx.showToast({
        title: '请输入新福利',
        icon:'none',
      })
      return false
    }
    let value = that.data.welfareval
    var gswelfare = that.data.gswelfare;
    var newData = {
      id:that.data.gswelfare.length,
      name:value,
      isSelected: false
    }
    gswelfare.push(newData);
    that.setData({gswelfare})
    that.setData({welfareval:''})
    console.log('备选',value,that.data.welfareval)
  },
  checkSensWords(e){
    console.log('敏感字',e)
    let itemData = e.target.dataset;
    console.log('敏感字',e.target.dataset)
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(e){
    var that = this
    var jiliid = that.data.id
    console.log('发布ID',jiliid)
    if(jiliid > 0){
      //编辑
      var method = 'PUT'
      var api = 'position/updatePosition'
      var jiliid = jiliid
    }else{
      // 添加
      var method = 'POST'
      var api = 'position/addPosition'
      var jiliid = 0
    }
    var jiekou = [];
    for (var i in that.data.gswelfare){
      if(that.data.gswelfare[i].isSelected){
        jiekou.push(that.data.gswelfare[i].name);
        that.data.fuliarr = jiekou
        that.data.fulistr = jiekou.join(',')
        this.data.fuliactive = true;
        console.log('最后提交福利数据',jiekou,that.data.fuliarr,that.data.fulistr)
      }
    }
    if(that.data.hangye == '' || that.data.hangye == '选择行业'){
      wx.showToast({
        title: '请填所属行业',
        icon:'none',
      })
      return false
    }
    if(that.data.positionName == '' || that.data.positionName == undefined){
      wx.showToast({
        title: '请填写职位名称',
        icon:'none',
      })
      return false
    }
    if(that.data.wageRange == ''){
      wx.showToast({
        title: '请选择薪资范围',
        icon:'none',
      })
      return false
    }
    if(that.data.saveProvince == '' || that.data.saveProvince == undefined){
      wx.showToast({
        title: '请选择省',
        icon:'none',
      })
      return false
    }
    if(that.data.saveCity == '' || that.data.saveCity == undefined){
      wx.showToast({
        title: '请选择市',
        icon:'none',
      })
      return false
    }
    if(that.data.address == '' || that.data.address == undefined){
      wx.showToast({
        title: '请填写上班地址',
        icon:'none',
      })
      return false
    }
    if(that.data.experience == ''){
      wx.showToast({
        title: '请选择工作经验',
        icon:'none',
      })
      return false
    }
    if(that.data.educational == ''){
      wx.showToast({
        title: '请选择最低学历',
        icon:'none',
      })
      return false
    }
    
    if(that.data.zprs == '' || that.data.zprs == undefined){
      wx.showToast({
        title: '请填写招聘人数',
        icon:'none',
      })
      return false
    }
    if(that.data.positionDuty == '' || that.data.positionDuty == undefined){
      wx.showToast({
        title: '请填写岗位职责',
        icon:'none',
      })
      return false
    }
    if(that.data.qualifications == '' || that.data.qualifications == undefined){
      wx.showToast({
        title: '请填写任职资格',
        icon:'none',
      })
      return false
    }
    console.log('福利是否选中',this.data.fuliactive)
    if(that.data.fuliactive == false){
      wx.showToast({
        title: '请选择公司福利（多选）',
        icon:'none',
      })
      return false
    }
    if(that.sensWords != undefined){
      let sensWords = Object.values(this.sensWords)
      if(sensWords.length > 0){
        wx.showToast({
          title: `${sensWords.join(',')}包含敏感词，请修改后再${that.data.sttsname}`,
          icon:'none',
        })
        return false;
      }
    }
    var data = {
      approval: '1',
      zprs:that.data.zprs,
      companyName:that.data.positiondata.companyName,
      companySize:that.data.positiondata.companySize,
      createTime:that.data.positiondata.createTime,
      enterpriseId:that.data.positiondata.enterpriseId,
      enterpriseNature:that.data.positiondata.enterpriseNature,
      isApply: that.data.positiondata.isApply,
      isCollection: that.data.positiondata.isCollection,
      isHot: that.data.positiondata.isHot,
      isRecommend: that.data.positiondata.isRecommend,
      jzyy: that.data.positiondata.jzyy,
      logo: that.data.positiondata.logo,
      positionInfo: that.data.positiondata.positionInfo,
      positionType: that.data.positiondata.positionType,
      qualifications: that.data.qualifications,
      recruitState: that.data.positiondata.recruitState,
      topStatus: that.data.positiondata.topStatus,
      updateTime: that.data.positiondata.updateTime,
      positionName:that.data.positionName,//职位名称
      positionDuty:that.data.positionDuty,//职位描述
      saveProvince: that.data.saveProvince,//省
      saveCity:that.data.saveCity,//市
      provinceCode: that.data.provinceCode,
      cityCode:that.data.cityCode,
      address:that.data.address,//详细地点
      experience:that.data.experience,//经验要求
      educational:that.data.educational,//最低学历
      wageRange:that.data.wageRange,//薪资范围
      enterpriseUid:that.data.enterpriseUid,//企业Uid
      id:jiliid,//工作经历ID
      industry:that.data.hangye,//所属行业
      welfareRemark:that.data.fulistr,
      // fuliarr:that.data.fuliarr
    }
    wx.request({
      url:url + api,
      header:{
      'context-type': 'application/json',
      },
      method: method,
      data:data,
      success:function(res){
        var ret = res.data;
        wx.removeStorageSync('hangye')//选择选中行业的缓存
        if(ret.code == 200){
            wx.showToast({
              title: '保存成功',
              icon:'none',
            })
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,
              })
            },1000)
        }else{
          wx.showToast({
            title: '保存失败',
            icon:'none',
          })
        }
      }
    })
  }
})